<template>
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>成绩查询</span>
        </div>
      </template>
      <el-form :model="form" label-width="100px">
        <el-form-item label="学号">
          <el-input v-model="form.studentId" placeholder="请输入学号"></el-input>
        </el-form-item>
        <el-form-item label="学期">
          <el-select v-model="form.semester" placeholder="请选择学期">
            <el-option label="第一学期" value="1"></el-option>
            <el-option label="第二学期" value="2"></el-option>
            <el-option label="第三学期" value="3"></el-option>
            <el-option label="第四学期" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryScores">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="scores" style="width: 100%" v-if="scores.length > 0">
        <el-table-column prop="courseName" label="课程名称" width="180"></el-table-column>
        <el-table-column prop="score" label="成绩" width="180"></el-table-column>
      </el-table>
      <div v-else-if="isError" class="error-message">查询失败，请检查输入信息。</div>
    </el-card>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        form: {
          studentId: '',
          semester: '',
        },
        scores: [],
        isError: false,
      };
    },
    methods: {
      async queryScores() {
        try {
          const response = await axios.post('/api/query-scores', this.form);
          this.scores = response.data.scores;
          this.isError = false;
        } catch (error) {
          console.error(error);
          this.scores = [];
          this.isError = true;
        }
      },
    },
  };
  </script>
  
  <style scoped>
  .box-card {
    width: 1360px;
    margin: 50px auto;
    background-color: rgba(94, 147, 170, 0.186);
    margin-left: 165px;
  }
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .error-message {
    color: rgb(87, 175, 118);
    margin-top: 20px;
  }
  </style>
  